Ravi Vishwakarma is a dedicated Software Developer with a passion for crafting efficient and innovative solutions. With a keen eye for detail and years of experience, he excels in developing robust software systems that meet client needs. His expertise spans across multiple programming languages and technologies, making him a valuable asset in any software development project.
ICSM Computer
02-Jun-2025To implement role-based authorization using Forms Authentication, you can control access by assigning roles to users and then:
[Authorize(Roles = "...")]attribute (in ASP.NET MVC or Web API), orweb.configfile (for Web Forms or general path-based protection).Step-by-Step Guide
Step 1: Assign Roles to the User on Login
When authenticating a user, create a
FormsAuthenticationTicketand embed roles in the ticket:Example (Login Code - Global.asax or Auth Controller)
Step 2: Extract Roles on Each Request (Global.asax)
Hook into
Application_AuthenticateRequestto extract the roles from the cookie and assign them to the current principal:Option 1: Use
[Authorize(Roles = "...")](MVC/Web API)Use the
[Authorize]attribute to restrict controllers or actions:Option 2: Use
<authorization>inweb.configUse this for restricting folders/pages (Web Forms or static files):
You can also restrict specific pages:
Summary
FormsAuthenticationTicketUserDataApplication_AuthenticateRequest[Authorize(Roles = "...")]web.config<authorization>